package com.research.web.controller;

import com.alibaba.druid.pool.DruidDataSource;
import com.research.web.config.db.DataSourceConfig;
import com.research.web.config.properties.ServiceConfig;
import com.research.web.entity.ServiceBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.cloud.context.refresh.ContextRefresher;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

@RestController
@Slf4j
@Api(tags = "手动刷新应用系统配置:目前都不可用")
public class DbController {
    @Autowired
    public List<DruidDataSource> dataSourceList;

    @Autowired
    private ContextRefresher contextRefresher;

    @GetMapping(path = "/refresh")
    public String refresh(){
        new Thread(() -> contextRefresher.refresh()).start();
        System.out.println(dataSourceList);
        return "ok";
    }

    @ApiOperation("获取jar所在目录路径")
    @RequestMapping(value = "/getJarHome", method = RequestMethod.GET)
    public String getJarHome() {
        return getJarHomePath();
    }

    private String getJarHomePath() {
        ApplicationHome ah = new ApplicationHome(getClass());
        File file = ah.getSource();
        String homePath = file.getParentFile().toString();
        System.out.println(homePath);
        return homePath;
    }

    @ApiOperation("用户手动更新dataSourceList")
    @RequestMapping(value = "/updateDataSourceListByConfig", method = RequestMethod.GET)
    public String updateDataSourceListByConfig() {
        try {
            int groupLength = 5;
            List<ServiceBean> list = new ArrayList<>();

            InputStream inputStream;
            String jarHomePath = getJarHomePath();
            String fileName = jarHomePath + "/service.properties";
            File file = new File(fileName);
            if (file.exists()) {
                // 优先读取当前jar所在目录下的配置文件
                inputStream = new FileInputStream(file);
            } else {
                ClassPathResource classPathResource = new ClassPathResource("service.properties");
                inputStream = classPathResource.getInputStream();
            }
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

            Properties properties = new Properties();
            properties.load(bufferedReader);

            int size = properties.size() / groupLength;
            for (int i = 0; i < size; i++) {
                ServiceBean serviceBean = new ServiceBean();
                serviceBean.setId(properties.getProperty("service.list[" + i + "].id"));
                serviceBean.setUrl(properties.getProperty("service.list[" + i + "].url"));
                serviceBean.setDriverClassName(properties.getProperty("service.list[" + i + "].driver-class-name"));
                serviceBean.setUsername(properties.getProperty("service.list[" + i + "].username"));
                serviceBean.setPassword(properties.getProperty("service.list[" + i + "].password"));
                list.add(serviceBean);
            }

            ServiceConfig serviceConfig = new ServiceConfig();
            serviceConfig.setList(list);
            dataSourceList = DataSourceConfig.getDataSourceList(serviceConfig);
            return "同步成功";
        } catch (Exception e) {
            log.error("同步失败，原因为：" + e);
            return "同步失败";
        }
    }
}
